#!/usr/bin/webif-page
<? 
. /usr/lib/webif/webif.sh
load_settings charon
. /etc/default/charon

if ! empty "$FORM_submit"; then
	if empty $FORM_generate; then
		validate <<EOF
ip|FORM_net|@TR<<IP of Charon Mirror>>|required|$FORM_net
ip|FORM_ff|@TR<<IP of Firefox Mirror>>|required|$FORM_ff
int|FORM_quantum|@TR<<Download Quantity>>|required|$FORM_quantum
EOF
		equal "$?" 0 && {
			SAVE_net=`echo $FORM_net | cut -d. -f1-3`
			[ "$SAVE_net" != "$CHARON_NET" ] && save_setting charon charon_net "$SAVE_net"
			[ "$FORM_ff" != "$CHARON_FF" ] && save_setting charon charon_ff "$FORM_ff"
			[ "$FORM_quantum" != "$CHARON_QUANTUM" ] && save_setting charon charon_quantum "$FORM_quantum"
			[ "$FORM_prepaid" != "$CHARON_PREPAID" ] && save_setting charon charon_prepaid "$FORM_prepaid"
			case "$FORM_enable" in 
				yes|no)
					[ "$FORM_enable" != "$CHARON_ENABLE" ] && save_setting charon charon_enable "$FORM_enable";;
			esac
			case "$FORM_currency" in 
				AUD|EUR|USD)
					[ "$FORM_currency" != "$CHARON_CURRENCY" ] && save_setting charon charon_currency "$FORM_currency";;
			esac
			if [ "$FORM_amount" != "$CHARON_AMOUNT" ]; then
				VALIDATED=`echo "$FORM_amount" | sed '/^[0-9][0-9]*\.[0-9][0-9]*$/{p;d}/^[0-9]*$/p;d'`
				if [ "$VALIDATED" = "$FORM_amount" ]; then
					save_setting charon charon_amount "$FORM_amount"
				else
					FORM_amount=${charon_amount:-$CHARON_AMOUNT}
				fi
			fi
		}
	fi
fi
	
header "Charon" "Settings" "@TR<<Charon Configuration>>" '' "$SCRIPT_NAME"

display_form <<EOF
start_form|@TR<<Key Creation>>
submit|generate|@TR<<Generate New Key>>
end_form
EOF

FORM_enable=${FORM_enable:-${charon_enable:-$CHARON_ENABLE}}
FORM_net=${FORM_net:-${charon_net:-$CHARON_NET}.0}
FORM_ff=${FORM_ff:-${charon_ff:-$CHARON_FF}}
FORM_quantum=${FORM_quantum:-${charon_quantum:-$CHARON_QUANTUM}}
FORM_currency=${FORM_currency:-${charon_currency:-$CHARON_CURRENCY}}
FORM_amount=${FORM_amount:-${charon_amount:-$CHARON_AMOUNT}}
FORM_prepaid=${FORM_prepaid:-${charon_prepaid:-$CHARON_PREPAID}}

if ! empty $FORM_generate; then
	openssl genrsa > /etc/key.rsa 2>/dev/null
	cat <<EOF
<FORM>
<TEXTAREA ROWS=`wc -l /etc/key.rsa` COLS=65 >
`cat /etc/key.rsa`
</TEXTAREA>
</FORM>
<P>Your newly generated key is above. Copy and paste it into a file somewhere safe! You may need it to withdraw the money you earn.</P>
EOF
fi


if [ -e /etc/key.rsa ]; then
	display_form <<EOF
start_form|@TR<<Charon Network Configuration>>
field|@TR<<ISP Status>>
select|enable|$FORM_enable
option|yes|@TR<<Enabled>>
option|no|@TR<<Disabled>>
field|@TR<<IP of Wireless Network>>
text|net|$FORM_net
field|@TR<<IP of Firefox Mirror>>
text|ff|$FORM_ff
field|@TR<<Download Charge>>
text|amount|$FORM_amount
select|currency|$FORM_currency
option|AUD|@TR<<Australian Dollars>>
option|EUR|@TR<<Euros>>
option|USD|@TR<<US Dollars>>
field|@TR<<Download Quantity>>
text|quantum|$FORM_quantum
end_form
EOF

	display_form <<EOF
start_form|@TR<<Charon Prepaid Configuration>>
field|@TR<<Prepaid instructions>>
text|prepaid|$FORM_prepaid
end_form
EOF
fi

echo "${ERROR}"
footer ?>

<!--
##WEBIF:name:Charon:1:Settings
-->
